<?php
namespace app\common\model;
class OrderOuterRoutes extends Order
{
	use \traits\ModelTrait;

	const TYPE_UNPAID     = 0;
	const TYPE_UNCONFIRM  = 1;
	const TYPE_UNGO       = 2;
	const TYPE_UNRATE     = 3;
	const TYPE_RATEED     = 4;
	const TYPE_DEPOSIT    = 5;

	//0:未支付 1:待确认 2:待出行 3:已出行 ，待评价 4:已评价 5：已定金
	public static $status = [
		0 => '未支付',
		1 => '待确认',
		2 => '待出行',
		3 => '待评价',
		4 => '已评价',
		5 => '已定金'
	];

	/**
	 * 订单列表
	 */
	public static function orderList($where)
	{
		$model = new self();
		$model->alias('ou');
		if ( isset($where['outer_status']) && $where['outer_status'] != '' ) {
		    $model->where(sprintf('ou.status = %d' , $where['outer_status']));
		}
		if ( isset($where['uid']) && $where['uid'] > 0 ) {
			$model->where(sprintf('m.uid = %d' , $where['uid']));
		}
		if ( isset($where['sales_admin_id']) && $where['sales_admin_id'] > 0 ) {
			$model->where(sprintf('m.uid IN (SELECT uid FROM `eb_user` WHERE sales_admin_id = %d)' , $where['sales_admin_id']));
		}
		if ( isset($where['mobile']) && $where['mobile'] != '' ) {
			$model->where("m.uid IN (SELECT uid FROM `eb_user` WHERE mobile LIKE '%" . $where['mobile'] . "%')");
		}
		$orderList = $model->field("ou.route_id,ou.sku_id,ou.date,ou.nums,ou.double_room,ou.big_room,ou.status as outer_status,router.route_name,sku.sku_name,m.id as order_id,m.order_no,m.out_trade_no,m.orgn_amt,m.disc_amt,m.order_amt,m.integral,m.golds,m.status,m.pay_type,m.pay_status,m.create_time,m.order_type,m.order_outer_id,m.spread_uid,ifnull(m.remark,'-') as remark,m.use_deposit,m.deposit_amt,m.final_amt,u.nickname,u.mobile")
		->join('__ORDER__ m','ou.id = m.order_outer_id AND m.order_type=' . self::ORDER_TYPE_ROUTES)
		->join('__USER__ u' , 'm.uid = u.uid')
		->join('__ROUTES_SKU__ sku' , 'ou.sku_id = sku.id')
		->join('__ROUTES__ router','ou.route_id = router.id')
		->order('m.create_time desc')
		->page($where['page'] ?? 1, $where['limit'] ?? static::LIMIT)
		->select();

		if ( !empty($orderList) ) {
			$orderList = $orderList->toArray();

			foreach ( $orderList as &$order ) {
				$order['outer_status_display'] = static::$status[$order['outer_status']] ?? '-';
				$order['status_display'] = parent::$status[$order['status']] ?? '-';
				$order['pay_status_display'] = parent::$pay_status[$order['pay_status']] ?? '-';
				$order['pay_type_display'] = parent::$pay_type[$order['pay_type']] ?? '-';
				$order['order_amt'] *= 1;
				$order['deposit_amt'] *= 1;
				$order['final_amt'] *= 1;
			}
		}
		return $orderList;
	}
}//Class End
